package com.cskaoyan.servlet;


import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.MarketRegion;
import com.cskaoyan.bean.vo.RegionVo;
import com.cskaoyan.service.RegionService;
import com.cskaoyan.service.RegionServiceImpl;
import com.cskaoyan.util.ResponseUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bianyonglei
 * @since 2023/05/27 14:43
 */
@WebServlet("/admin/region/*")
public class AdminRegionServlet extends WdBaseServlet {
   private RegionService regionService =  new RegionServiceImpl();
   @SneakyThrows
   private void list(HttpServletRequest request, HttpServletResponse response){
      List<RegionVo> regionVoList = new ArrayList<>();
      //List<MarketRegion> marketRegions = regionService.getAll();
      List<MarketRegion> marketRegions = regionService.getAll();

      Map<Byte, List<MarketRegion>> collect = marketRegions.stream().collect(Collectors.groupingBy(MarketRegion::getType));
      byte provinceType = 1;
      byte cityType = 2;
      byte areaType = 3;
      List<MarketRegion> provinceList = collect.get(provinceType);
      List<MarketRegion> cityList = collect.get(cityType);
      List<MarketRegion> areaList = collect.get(areaType);

      // key为省份id，value为对应的cityList
      Map<Integer, List<MarketRegion>> cityMap = cityList.stream().collect(Collectors.groupingBy(MarketRegion::getPid));
      // key为市id，value为对应的areaList
      Map<Integer, List<MarketRegion>> areaMap = areaList.stream().collect(Collectors.groupingBy(MarketRegion::getPid));

      for (MarketRegion province : provinceList) {
         RegionVo provinceVo = new RegionVo();
         provinceVo.setId(province.getId());
         provinceVo.setName(province.getName());
         provinceVo.setCode(province.getCode());
         provinceVo.setType(province.getType());


         List<MarketRegion> currentcityList = cityMap.get(province.getId());
         List<RegionVo> cityVoList = new ArrayList<>();

         for (MarketRegion city : currentcityList) {
            RegionVo cityVo = new RegionVo();
            cityVo.setId(city.getId());
            cityVo.setName(city.getName());
            cityVo.setCode(city.getCode());
            cityVo.setType(city.getType());


//            Integer cityId = city.getId();
            List<MarketRegion> currentAreaList = areaMap.get(city.getId());
            if (currentAreaList == null) {
               continue;
            }

            List<RegionVo> areaVoList = new ArrayList<>();
            for (MarketRegion currentArea : currentAreaList) {
               RegionVo areaVo = new RegionVo();
               areaVo.setId(currentArea.getId());
               areaVo.setName(currentArea.getName());
               areaVo.setType(currentArea.getType());
               areaVo.setCode(currentArea.getCode());

               areaVoList.add(areaVo);
            }

            cityVo.setChildren(areaVoList);
            cityVoList.add(cityVo);
         }
         provinceVo.setChildren(cityVoList);
         regionVoList.add(provinceVo);
      }
      ResponseUtil.responseJson(response, BaseRespVo.ok(CommonData.data(regionVoList)));
      //返回值类型是BaseRespo  用return BaseRespVo.ok(CommonData.data(regionVoList));

   }

}
